package cn.edu.hqu.pre;

import org.uma.jmetal.solution.AbstractSolution;
import org.uma.jmetal.solution.Solution;
import org.uma.jmetal.solution.permutationsolution.PermutationSolution;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.IntStream;

/**
 * 一日游、两日游、三日游模型的解的生成方案
 * 以每次移除热度最低的遗产点后的解编码的随机排列作为解的生成方案
 */
public class MyIntegerPermutationSolution extends AbstractSolution<Integer>
        implements PermutationSolution<Integer> {

    /**
     * Constructor
     */
    public MyIntegerPermutationSolution(int permutationLength, int numberOfObjectives, int numberOfConstraints) {
        super(permutationLength, numberOfObjectives, numberOfConstraints);

        List<Integer> randomSequence = new ArrayList<>(CodeHelp.codeMap.values());

        java.util.Collections.shuffle(randomSequence);

        IntStream.range(0, permutationLength).forEach(i -> variables().set(i, randomSequence.get(i)));
    }

    /**
     * Copy Constructor
     */
    public MyIntegerPermutationSolution(MyIntegerPermutationSolution solution) {
        super(solution.getLength(), solution.objectives().length, solution.constraints().length);

        IntStream.range(0, solution.variables().size()).forEach(i -> variables().set(i, solution.variables().get(i)));
        IntStream.range(0, solution.objectives().length).forEach(i -> objectives()[i] = solution.objectives()[i]);
        IntStream.range(0, solution.constraints().length).forEach(i -> constraints()[i] = solution.constraints()[i]);

        attributes = new HashMap<>(solution.attributes);
    }

    @Override
    public int getLength() {
        return variables().size();
    }

    @Override
    public Solution<Integer> copy() {
        return new MyIntegerPermutationSolution(this);
    }
}
